set scheme stcolor

use ../Metadata/tess_analysisdata.dta, clear

********************************************************************************

* median sample size per category
preserve
keep if hyp_num==1
egen samplesizecatmed=median(samplesize), by(samplesizecat)
tab samplesizecat samplesizecatmed
restore

* create frame
cap frame change default
cap frame drop results
frame create results strL varname mean se ul ll str8 N


* Sample size, study level

	* study level analysis
	preserve
	keep if hyp_num==1

	proportion successfulexp_insample if samplesize<=1000, level(83.4)
	mat table = r(table)	
	local mean = table[1,2]
	local se = table[2,2]
	local ll = table[5,2]
	local ul = table[6,2]
	local N	 = table[7,1]+1		
	frame post results ("1000 or less") (`mean') (`se') (`ll') (`ul') ("N=`N'")
		
	proportion successfulexp_insample if samplesize>=1001 & samplesize<=2000, level(83.4)		
	mat table = r(table)	
	local mean = table[1,2]
	local se = table[2,2]
	local ll = table[5,2]
	local ul = table[6,2]
	local N	 = table[7,1]+1		
	frame post results ("1001-2000") (`mean') (`se') (`ll') (`ul') ("N=`N'")

	proportion successfulexp_insample if samplesize>=2001 & samplesize<=3000, level(83.4)		
	mat table = r(table)	
	local mean = table[1,2]
	local se = table[2,2]
	local ll = table[5,2]
	local ul = table[6,2]
	local N	 = table[7,1]+1		
	frame post results ("2001-3000") (`mean') (`se') (`ll') (`ul') ("N=`N'")
	
	proportion successfulexp_insample if samplesize>=3001, level(83.4)	
	mat table = r(table)	
	local mean = table[1,2]
	local se = table[2,2]
	local ll = table[5,2]
	local ul = table[6,2]
	local N	 = table[7,1]+1		
	frame post results ("Over 3001") (`mean') (`se') (`ll') (`ul') ("N=`N'")	
			
	restore

* Sample size, hyp level, all insample tests

	proportion hyp_true if N_person<=1000 & insample==1, level(83.4)
	mat table = r(table)	
	local mean = table[1,2]
	local se = table[2,2]
	local ll = table[5,2]
	local ul = table[6,2]
	local N	 = table[7,1]+1		
	frame post results ("1000 or less") (`mean') (`se') (`ll') (`ul') ("N=`N'")
		
	proportion hyp_true if N_person>=1001 & N_person<=2000& insample==1, level(83.4)		
	mat table = r(table)	
	local mean = table[1,2]
	local se = table[2,2]
	local ll = table[5,2]
	local ul = table[6,2]
	local N	 = table[7,1]+1		
	frame post results ("1001-2000") (`mean') (`se') (`ll') (`ul') ("N=`N'")

	proportion hyp_true if N_person>=2001 & N_person<=3000& insample==1, level(83.4)		
	mat table = r(table)	
	local mean = table[1,2]
	local se = table[2,2]
	local ll = table[5,2]
	local ul = table[6,2]
	local N	 = table[7,1]+1		
	frame post results ("2001-3000") (`mean') (`se') (`ll') (`ul') ("N=`N'")
	
	proportion hyp_true if N_person>=3001& insample==1, level(83.4)	
	mat table = r(table)	
	local mean = table[1,2]
	local se = table[2,2]
	local ll = table[5,2]
	local ul = table[6,2]
	local N	 = table[7,1]+1		
	frame post results ("Over 3001") (`mean') (`se') (`ll') (`ul') ("N=`N'")	


	
* graph
frame change results
gen xlabel=_n
labmask xlabel, values(varname)
gen mlabel=0.1


* hypothesis-level	
		graph twoway ///
		(bar mean xlabel if xlabel>4&xlabel<=8, barw(0.8) fc(stc1%50) colordiscrete lc(none)) || ///
		(rcap ll ul xlabel if xlabel>4&xlabel<=8,lc(gs7)) ///
		(scatter mlabel xlabel if _n>4&_n<=8, ///		
msym(none) mlab(N) mlabpos(6) mlabcolor(black) mlabsize(3.8) mlabangle(hor)) ///
		, ///		
			ytitle("Pr(supported hypothesis)", size(4.5) margin(r=2 l=-2)) ///
			xlabel( ///
			, labsize(4) angle(ver) valuelabels) ///
			xtitle("Test sample size", margin(b=0) size(4.5)) ///
			ylabel(0(0.2)1, labsize(4) nogrid) ///
			title("{it:Hypotheses}", size(5)) ///
			scheme(s2mono) ///
			graphregion(color(white)) ///
			legend(off) ///
			xsize(2) ysize(3) 
			
			graph export ../Results/Figure2_left.tif, replace


* study-level
		graph twoway ///
		(bar mean xlabel if xlabel<=4, barw(0.8) fc(stc2%50) colordiscrete lc(none)) || ///
		(rcap ll ul xlabel if xlabel<=4,lc(gs7)) ///
		(scatter mlabel xlabel if _n<=4, ///		
msym(none) mlab(N) mlabpos(6) mlabcolor(black) mlabsize(3.8) mlabangle(hor)) ///
		, ///	
			ytitle("Pr(positive result)", size(4.5) margin(r=2 l=-2)) ///
			xlabel( ///
			, labsize(4) angle(ver) valuelabels) ///
			xtitle("Study sample size", margin(b=0) size(4.5)) ///
			ylabel(0(0.2)1, labsize(4) nogrid) ///
			title("{it:Studies}", size(5)) ///
			scheme(s2mono) ///
			graphregion(color(white)) ///
			legend(off) ///
			xsize(2) ysize(3)
			
			graph export ../Results/Figure2_right.tif, replace
	
			
		frame change default	
